package com.jf.thread;

import java.util.ArrayList;
import java.util.List;

public class BaoZiPu {
    public int count = 0;
    private List<String> list = new ArrayList<>();

    /*制作包子*/
    public synchronized void setBaozi(String name){
        count++;
        if(count <= 100){
            System.out.println("制作的第["+count+"]个包子");
            list.add(name);
        }
        notifyAll();/*唤醒等待的线程*/
    }

    public synchronized String getBaozi(){
        if(list.isEmpty()){
            try {
                wait();// 等待
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        // 等待后被唤醒会直接执行该代码，但是这不能保证list一定有值！
        return list.remove(0);
    }
}
